package com.dcy.znoa.common.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author pengyu
 * @version 2017/7/6
 * @see
 */
public class DateUtil {
    public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat datetimeFormat_1 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat datetimeFormat_MHS= new SimpleDateFormat("HH:mm:ss");
    public static final SimpleDateFormat datetimeFormat_ms = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    /**
     * 时间段是否交叉  交叉返回true
     * @param leftStartDate
     * @param leftEndDate
     * @param rightStartDate
     * @param rightEndDate
     * @return
     */
    public static boolean isOverlap(Date leftStartDate, Date leftEndDate, Date rightStartDate, Date rightEndDate) {

        return
                ((leftStartDate.getTime() >= rightStartDate.getTime())
                        && leftStartDate.getTime() < rightEndDate.getTime())
                        ||
                        ((leftStartDate.getTime() > rightStartDate.getTime())
                                && leftStartDate.getTime() <= rightEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() >= leftStartDate.getTime())
                                && rightStartDate.getTime() < leftEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() > leftStartDate.getTime())
                                && rightStartDate.getTime() <= leftEndDate.getTime());

    }

    /**
     * 获取当前时间
     * @param datetimeFormat
     * @return
     */
    public static String getFormatNowDate(SimpleDateFormat datetimeFormat) {
        Date date = new Date();
        return datetimeFormat.format(date);
    }

    /**
     * 获取当前时间
     * @param
     * @return
     */
    public static String getFormatNowDate1() {
        Date date = new Date();
        return datetimeFormat_1.format(date);
    }

    /**
     * 按经典年月日时分秒格式化时间
     * @param
     * @return
     */
    public static String getFormatDate(Date date) {
        if(date==null){
            return "";
        }
        return datetimeFormat.format(date);
    }

    /**
     * 获取当前时间 - 年月日
     * @param
     * @return
     */
    public static String getNowDateYMD() {
        Date date = new Date();
        return dateFormat.format(date);
    }

    /**
     * 获取昨天 - 年月日 String
     * @param
     * @return
     */
    public static String getYesterdayYMD() {
        Date now = new Date();
        //获取昨天时间
        Date backupTime= DateUtils.addDays(now, -1);
        return dateFormat.format(backupTime);
    }


    /**
     * 获取昨天 - 年月日 Date
     * @param
     * @return
     */
    public static Date getYesterday() {
        Date now = new Date();
        //获取昨天时间
        Date backupTime= DateUtils.addDays(now, -1);
        return backupTime;
    }

    public static String toString(Object[] a) {
        if (a == null)
            return "";

        int iMax = a.length - 1;
        if (iMax == -1)
            return "";

        StringBuilder b = new StringBuilder();
        b.append("");
        for (int i = 0; ; i++) {
            b.append(String.valueOf(a[i]));
            if (i == iMax)
                return b.append("").toString();
            b.append(",");
        }
    }

    public static void main(String args[]){
        System.out.print(getNowDateYMD());
    }

}
